knitr::opts_chunk$set(tidy = FALSE
,cache = FALSE
,fig.height = 8
,fig.width = 11)
library(tidyverse)## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag(): dplyr, stats
library(data.table)## data.table 1.9.6 For help type ?data.table or https://github.com/Rdatatable/data.table/wiki
## The fastest way to learn (by data.table authors): https://www.datacamp.com/courses/data-analysis-the-data-table-way
## -------------------------------------------------------------------------
## data.table + dplyr code now lives in dtplyr.
## Please library(dtplyr)!
## -------------------------------------------------------------------------
##
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
##
## between, last
## The following object is masked from 'package:purrr':
##
## transpose
library(dtplyr)
library(ggfortify)
library(plotly)##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
options(width = 80L)
set.seed(42)ap_decompose <- decompose(AirPassengers)
autoplot(ap_decompose)## Warning: attributes are not identical across measure variables; they will be
## dropped
## Warning: Removed 24 rows containing missing values (geom_path).
Let’s try a multiplicative model.
ap_decompose_mult <- decompose(AirPassengers, type = 'multiplicative')
autoplot(ap_decompose_mult)## Warning: attributes are not identical across measure variables; they will be
## dropped
## Warning: Removed 24 rows containing missing values (geom_path).
ap_decompose_stl <- stl(AirPassengers, s.window = 'periodic')
autoplot(ap_decompose_stl)autoplot(AirPassengers) +
ggtitle("Plot of the Air Passengers")autoplot(diff(AirPassengers)) +
ggtitle("Plot of the Diffs of the Air Passengers")ap_decomp_resid <- ap_decompose$random[!is.na(ap_decompose$random)]
ap_acf <- acf(ap_decomp_resid, plot = FALSE)
autoplot(ap_acf) +
ggtitle("Correlogram of the Air Passenger Residuals")ap_pacf <- pacf(ap_decomp_resid, plot = FALSE)
autoplot(ap_pacf) +
ggtitle("Partial Correlogram of the Air Passenger Residuals")autoplot(pacf(diff(AirPassengers), plot = FALSE)) +
ggtitle("Partial Correlogram of the Differenced Air Passenger Data")ma_1 <- arima.sim(list(ma = 0.8), innov = innovations, n = 100)
output_plot <- ggplot() +
geom_line(aes(x = seq_along(innovations[1:100]), y = innovations[1:100]), size = 0.5) +
geom_line(aes(x = seq_along(ma_1), y = as.numeric(ma_1)), colour = 'red') +
xlab("Time Step") +
ylab("Value")
ggplotly(output_plot)ma_2 <- arima.sim(list(ma = c(0.4, 0.4)), innov = innovations, n = 100)
output_plot <- ggplot() +
geom_line(aes(x = seq_along(innovations[1:100]), y = innovations[1:100]), size = 0.5) +
geom_line(aes(x = seq_along(ma_2), y = as.numeric(ma_2)), colour = 'red') +
xlab("Time Step") +
ylab("Value")
ggplotly(output_plot)output_plot <- ggplot() +
geom_line(aes(x = seq_along(innovations[1:100]), y = innovations[1:100]), size = 0.5) +
geom_line(aes(x = seq_along(ma_1), y = as.numeric(ma_1)), colour = 'red') +
geom_line(aes(x = seq_along(ma_2), y = as.numeric(ma_2)), colour = 'blue') +
xlab("Time Step") +
ylab("Value")
ggplotly(output_plot)Now that we have created MA series, we look at what the AR series look like.
ar_1 <- arima.sim(list(ar = 0.8), innov = innovations, n = 100)
output_plot <- ggplot() +
geom_line(aes(x = seq_along(innovations[1:100]), y = innovations[1:100]), size = 0.5) +
geom_line(aes(x = seq_along(ar_1), y = as.numeric(ar_1)), colour = 'red') +
xlab("Time Step") +
ylab("Value")
ggplotly(output_plot)ar_2 <- arima.sim(list(ar = c(0.4, 0.4)), innov = innovations, n = 100)
output_plot <- ggplot() +
geom_line(aes(x = seq_along(innovations[1:100]), y = innovations[1:100]), size = 0.5) +
geom_line(aes(x = seq_along(ar_2), y = as.numeric(ar_2)), colour = 'red') +
xlab("Time Step") +
ylab("Value")
ggplotly(output_plot)output_plot <- ggplot() +
geom_line(aes(x = seq_along(innovations[1:100]), y = innovations[1:100]), size = 0.5) +
geom_line(aes(x = seq_along(ar_1), y = as.numeric(ar_1)), colour = 'red') +
geom_line(aes(x = seq_along(ar_2), y = as.numeric(ar_2)), colour = 'blue') +
xlab("Time Step") +
ylab("Value")
ggplotly(output_plot)arma_1_1 <- arima.sim(list(ar = 0.4, ma = 0.4), innov = innovations, n = 100)
output_plot <- ggplot() +
geom_line(aes(x = seq_along(innovations[1:100]), y = innovations[1:100])) +
geom_line(aes(x = seq_along(ar_1), y = as.numeric(ar_1)), colour = 'red') +
geom_line(aes(x = seq_along(ma_1), y = as.numeric(ma_1)), colour = 'blue') +
geom_line(aes(x = seq_along(arma_1_1), y = as.numeric(arma_1_1)), colour = 'green') +
xlab("Time Step") +
ylab("Value")
ggplotly(output_plot)